<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneNum"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				<wInput
					v-model="yzm"
					type="number"
					maxlength="6"
					placeholder="验证码"
					
					isShowCode
					ref="runCode"
					@setCode="getCode"
				></wInput>
			</view>
			
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="sendCode"
				class="wbutton"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="weixinLo"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import statusBar from '@/components/statusBar';
	import wInput from '@/components/watch-login/watch-input.vue'; //输入框
	import wButton from '@/components/watch-login/watch-button.vue'; //按钮
	import loginVue from './login.vue';
	// import navigate from '@/components/navigate';
	// import {
	// 	mapState,
	// 	mapMutations
	// } from 'vuex';
	export default {
		components: {
			statusBar,
			wInput,
			wButton
		},
		data() {
			return {
				btnImgSrc: "../../../static/icon/my_module/login/weixin.png",
				logoImage: "../../../static/96x96.png",
				phoneNum: '',
				yzm: '',
				password: '',
				btnMsg: '',
				statusBarHeight: 0,
				isShow: true,
				disable: true,
				count: '获取验证码',
				timer: null,
				providerList: [],
				userInfo: {},
				serverurl: "",
				show: false,
				type: '',
				privacy: false,
				pushmessage: {
					"type": "pushmessage",
					"toopenid": "88823e4c-6c3e-11ea-b779-fdb4d4144657",
					"message": "您已认证成功！",
					"time": "1585999577",
					"noreadnumber": 0
				},
				pushmessageForNo: {
					"type": "pushmessage",
					"toopenid": "88823e4c-6c3e-11ea-b779-fdb4d4144657",
					"message": "认证失败！",
					"time": "1585999577",
					"noreadnumber": 0
				},
				oldcer: 0,
				show: false,
				type: '',
				isRotate: false, //是否启动加载

			};
		},
		onLoad() {
			//把当前日期变成时间戳
			var time = (new Date()).getTime();
			time = time + '0';
			time = time.substring(0, 10);
			this.pushmessage.time = time; //离线时认证消息推送
			this.serverurl = getApp().globalData.serverUrl;
			console.log(getApp().globalData)
			// #ifdef APP-PLUS
			// 获取手机状态栏高度
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			// #endif
			var userInfo = this.getGlobalUser("globalUser");
			var usertoken = uni.getStorageSync("usertoken");
			var flag = usertoken ? true : false;
			if (flag) {
				this.sendToken();
			}
		},
		methods: {
			jump() {
				uni.switchTab({
					url: '../../news/index'

				})
			},
			jump_privacy() { //跳转到用户协议
				uni.navigateTo({
					url: "../set/privacy/privacy"
				})
			},
			getAddress(openid){
				console.log(openid)
				let me=this;
				var usertoken = uni.getStorageSync("usertoken");
				uni.getLocation({
				    type: 'wgs84',
				    success: function (res) {
				        console.log('当前位置的经度：' + res.longitude);
				        console.log('当前位置的纬度：' + res.latitude);
						uni.request({
							url: me.serverurl + "/user/submitAddress",
							data: {
								"longitude": res.longitude,
								"latitude":res.latitude,
								"openid":openid
							},
							header: {
								"usertoken": usertoken
							},
							method: "POST",
							success(result) {
								console.log(result)
							
							},
							fail: () => {
								uni.showToast({
									duration: 1000,
									title: "获取验证码失败"
								})
							}
						})
				    }
				});
			},
			weixinLo() {
				// uni.navigateTo({
				// 	url:'../bind/bind'
				// })
				// return ;
				let that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						uni.request({
							url: that.serverurl + "/user/we_chat/login",
							data: {
								"authResult":loginRes.authResult
							},
							method: "POST",
							success(result) {
								if (result.data.status == 200) {
									console.log(result)
									uni.setStorageSync("healthInfo", result.data.healthinfo);
									uni.setStorageSync("accountInfo", result.data.accountinfo);
									uni.setStorageSync("usertoken", result.data.token);
									uni.setStorageSync("connectBlue",false);
									uni.setStorageSync("sendBlue",false);
									that.getAddress(result.data.accountinfo.openid);
									uni.switchTab({
										url: '../../news/index'
									})
									uni.showToast({
										duration: 1000,
										title: "登录成功"
									})
									// me.sendCode();
								}else if(result.data.status == 1){
									uni.showToast({
										icon: 'none',
										title: result.data.message
									})
									uni.navigateTo({
										url:'../bind/bind?openid='+ loginRes.authResult.openid
									})
								}
							},
							fail: () => {
								uni.showToast({
									duration: 1000,
									title: "获取验证码失败"
								})
							}
						})
					}
				});
			},
			Code() {
				if (this.phoneNum.lengsth < 11) {
					uni.showToast({
						title: "请输入有效手机号"
					})
					return;
				}
				var me = this;

				//获取验证码
				uni.request({
					url: me.serverurl + "/user/getyzm",
					data: {
						"phone": me.phoneNum
					},
					method: "POST",
					success(result) {
						console.log('result 232', result);

						if (result.data.status == 200) {
							uni.showToast({
								duration: 1000,
								title: "发送验证码成功"
							})
							// me.sendCode();
							me.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
						}
					},
					fail: () => {
						uni.showToast({
							duration: 1000,
							title: "获取验证码失败"
						})
					}
				})
			},
			sendCode() { //发送验证码验证并登录
				var me = this;
				//获取验证码
				var loginInfo = {};
				loginInfo.phoneNumber = me.phoneNum;
				loginInfo.yzmNumber = me.yzm;
				uni.showLoading({
					title: "验证登录中"
				});
				uni.request({
					url: me.serverurl + "/user/tele/login",
					data: {
						"phone": me.phoneNum,
						"yzm": me.yzm
					},
					method: "POST",
					success(result) {
						if (result.data.status == 200) {
							console.log(result.data);
							
							uni.setStorageSync("healthInfo", result.data.healthinfo);
							uni.setStorageSync("accountInfo", result.data.accountinfo);
							uni.setStorageSync("usertoken", result.data.token);
							uni.setStorageSync("connectBlue",false);
							uni.setStorageSync("sendBlue",false);
							me.getAddress(result.data.accountinfo.openid);
							uni.switchTab({
								url: '../../news/index'
							})
							uni.showToast({
								duration: 1000,
								title: "登录成功"
							})

						} else if (result.data.status == 401) {
							uni.hideLoading();
							uni.showToast({
								duration: 1000,
								icon: "none",
								title: "token已过期,请重新登录"
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								duration: 1000,
								icon: 'error',
								// image: "../../static/images/fabu/error.png",
								title: "登录失败"
							})
						}
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							duration: 1000,
							image: "../../static/images/fabu/error.png",
							title: "登录失败"
						})
					}
				})
			},
			getGlobalUser() { //nvue无法加载main.js函数
				var userInfo = uni.getStorageSync("globalUser");
				if (userInfo != null && userInfo != "" && userInfo != undefined) {
					return userInfo;
				} else {
					return null;
				}
			},
			// 登录方式切换
			loginChange() {
				this.isShow = !this.isShow;
			},
			// 账号登录获取验证码
			getCode() {
				if (this.phoneNum.length < 11) {
					uni.showToast({
						title: "手机号无效！",
						image: "../../static/images/fabu/error.png"
					})
					return;
				}
				this.btnMsg = 's重发';
				this.disable = false;
				const TIME_COUNT = 60;
				
				this.Code();
				// if (!this.timer) {
				// 	this.count = TIME_COUNT;
				// 	this.timer = setInterval(() => {
				// 		if (this.count > 0 && this.count <= TIME_COUNT) {
				// 			this.count--;
				// 		} else {
				// 			clearInterval(this.timer);
				// 			this.timer = null;
				// 			this.disable = true;
				// 			this.count = '重新获取';
				// 			this.btnMsg = '';
				// 		}
				// 	}, 1000);
				// 	this.Code();
				// 	// this.showToast('发送成功', 'success');
				// }
			},
			sendToken() { //发送token并登录
				var me = this;
				//获取验证码
				var loginInfo = {};
				var usertoken = uni.getStorageSync("usertoken");
				uni.showLoading({
					title: "验证登录中"
				});
				uni.request({
					url: me.serverurl + "/user/token/login",
					data: {},
					header: {
						"usertoken": usertoken
					},
					method: "POST",
					success(result) {
						// console.log(result.data)
						if (result.data.status == 200) {
							
							uni.setStorageSync("healthInfo", result.data.healthinfo);
							uni.setStorageSync("accountInfo", result.data.accountinfo);
							uni.setStorageSync("usertoken", result.data.token);
							uni.setStorageSync("connectBlue",false);
							uni.setStorageSync("sendBlue",false);
							me.getAddress(result.data.accountinfo.openid);
							uni.switchTab({
								url: '../../news/index'
							})
							uni.showToast({
								duration: 1000,
								title: "登录成功"
							})

						} else if (result.data.status == 401) {
							uni.hideLoading();
							uni.showToast({
								title: 'token已过期,请重新登录',
								icon: 'none'
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								duration: 1000,
								icon: "error",
								// image: "../../static/images/fabu/error.png",
								title: "登录失败"
							})
						}
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							duration: 1000,
							title: "登录失败"
						})
					}
				})
			},
			// 消息弹框
			showToast(title, icon) {
				uni.showToast({
					title: String(title),
					icon: String(icon)
				});
			}
		}
	};
</script>

<style>
	@import url("./main.css");
	@import url("./icon.css");
</style>
